import 'dart:io';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

import '../constant/channel_constant.dart';
import '../constant/event_bus_global.dart';
import '../helper/event_manager.dart';
import '../model/ble.dart';
import '../utils/log_utils.dart';
//
//调用原生方法
//

class NativeUtils {
  ///初始化 柔灵 SDK
  static void nativeInitFlexSDKListener() async {
    flexSDKEventChannel.receiveBroadcastStream().listen((content) {
      //返回的内容
      LogUtils.log('flutter 端接收到=$content');
    }, onError: (Object event) {
      //返回的内容
    });
  }
  ///android 获取权限
  static Future<bool> requestPermission() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventOpenPermission);
    } on Exception catch (e) {
      debugPrint(e.toString());
    }
    return result;
  }

  // com.flexolink.wch/aio/location
  static void startIOSLocation() {
    const platform = MethodChannel('com.flexolink.wch/aio/location');
    platform.invokeMethod('location');

  }

  ///扫描监听
  static void nativeScanListener() async {
    Map map = {"action": "scan"};
    bleEventChannel.receiveBroadcastStream(map).listen((content) {
      //返回的内容
      LogUtils.log('flutter 端接收到=$content');
      if (content is Map) {
        String channel = content['channelName'];
        int code = content['code'];
        String deviceName = content['deviceName'];
        bool isConnected = content['isConnected'];
        String? mac = content['mac'];
        BleModel model = BleModel(channelName: channel, code: code, deviceName: deviceName, isConnected: isConnected, mac: mac);
        globalEventBus.fire(model);
      } else {
      }
    }, onError: (Object event) {
      //返回的内容
    });
  }
  ///停止扫描
  static void nativeStopScanListener() async {
    Map map = {"action": "stopScan"};
    bleEventChannel.receiveBroadcastStream(map).listen((content) {
      //返回的内容
      if (content is bool) {

      } else {
      }
    }, onError: (Object event) {
      //返回的内容
    });
  }
  ///连接监听
  static void nativeConnectListener(String deviceName, String? mac) async {
    Map map = {"action":"connectDevice", "deviceName": deviceName};
    if (mac != null) {
      map.addAll({"mac": mac});
    }
    bleEventChannel.receiveBroadcastStream(map).listen((content) {
      //返回的内容
      if (content is Map) {
        String channel = content['channelName'];
        int code = content['code'];
        String deviceName = content['deviceName'];
        bool isConnected = content['isConnected'];
        String? mac = content['mac'];
        BleModel model = BleModel(channelName: channel, code: code, deviceName: deviceName, isConnected: isConnected, mac: mac);
        globalEventBus.fire(model);

      } else {
      }
    }, onError: (Object event) {
      //返回的内容
    });
  }
  ///扫描一体机
  static Future<bool> scanAIO() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventScanAIO);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///停止扫描一体机
  static Future<bool> stopScanAIO() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventStopScanAIO);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///监听手机蓝牙开启状态
  static Future<bool> bleCentralState() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventBleCentralState);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///连接一体机
  static Future<bool> connectAIO({arguments = Map<String, String>}) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventConnectAIO, arguments);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///断连一体机
  static Future<bool> disconnectAIO({arguments = Map<String, String>}) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventDisConnectAIO);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///原生解码 验签
  static Future<bool> nativeEndianValid({arguments = Map<String, String>}) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventEndianUser, arguments);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///原生解码 配网
  static Future<bool> nativeEndianWifi({arguments = Map<String, String>}) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventEndianWifi, arguments);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///app 升级
  static Future<bool> appUpgrade (Map<String, String> arguments) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventAppUpgrade, arguments);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///app 获取 wifi 名称
  static Future<bool> getWifiName () async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(getEventWifi);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///发送指令给一体机
  static Future<bool> nativeAIOListener(List<int> cmdList) async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventBluetooth, cmdList);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///跳转到 app 系统设置的推送管理
  static Future<bool> jumpAppManagerPush() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventJumpAppManagerPush);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///打开 app 应用管理的推送开关的通知
  static Future<bool> openPushNoti() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventOpenPushNoti);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  ///关闭 app 应用管理的推送开关的通知
  static Future<bool> closePushNoti() async {
    //监听原生方法
    bool result = false;
    try {
      result = await scanAIOMethodChannel.invokeMethod(eventClosePushNoti);
    } on Exception catch (e) {
      LogUtils.log(e.toString());
    }
    return result;
  }
  //
  // ///android 获取权限
  // static Future<bool> requestPermission() async {
  //   //监听原生方法
  //   bool result = false;
  //   try {
  //     result = await androidRequestPermission.invokeMethod(eventOpenPermission);
  //   } on Exception catch (e) {
  //     LogUtils.log(e.toString());
  //   }
  //   return result;
  // }

}